package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdEditorialStatus;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdStatus;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdType;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.MobileAd;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class GxMobileAd extends MobileAd {
    private long gxId;

    public GxMobileAd(Element element) throws IllegalArgumentException, GxItemException {

        // Ad parameters

        if (element.getElementsByTagName("EditorialStatus").getLength() > 0 &&
            element.getElementsByTagName("EditorialStatus").item(0).getChildNodes().getLength() > 0) {
            this.setEditorialStatus(AdEditorialStatus.fromString(((Node) element.getElementsByTagName("EditorialStatus").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("GxMobileAdId").getLength() > 0 &&
            element.getElementsByTagName("GxMobileAdId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxMobileAdId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxMobileAdId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external MobileAd id");
        }
        if (element.getElementsByTagName("BingMobileAdId").getLength() > 0 &&
            element.getElementsByTagName("BingMobileAdId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingMobileAdId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingMobileAdId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Status").getLength() > 0 &&
                element.getElementsByTagName("Status").item(0).getChildNodes().getLength() > 0) {
            if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("E")) {
                this.setStatus(AdStatus.Active);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("D")) {
                this.setStatus(AdStatus.Deleted);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("I")) {
                this.setStatus(AdStatus.Inactive);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("P")) {
                this.setStatus(AdStatus.Paused);
            }
        }
        if (element.getElementsByTagName("Type").getLength() > 0 &&
            element.getElementsByTagName("Type").item(0).getChildNodes().getLength() > 0) {
            this.setType(AdType.fromString(((Node) element.getElementsByTagName("Type").item(0).getChildNodes().item(0)).getNodeValue()));
        }

        // MobileAd parameters

        if (element.getElementsByTagName("DestinationUrl").getLength() > 0 &&
                element.getElementsByTagName("DestinationUrl").item(0).getChildNodes().getLength() > 0) {
            this.setDestinationUrl(((Node) element.getElementsByTagName("DestinationUrl").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("BusinessName").getLength() > 0 &&
            element.getElementsByTagName("BusinessName").item(0).getChildNodes().getLength() > 0) {
            this.setBusinessName(((Node) element.getElementsByTagName("BusinessName").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("DisplayUrl").getLength() > 0 &&
            element.getElementsByTagName("DisplayUrl").item(0).getChildNodes().getLength() > 0) {
            this.setDisplayUrl(((Node) element.getElementsByTagName("DisplayUrl").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("PhoneNumber").getLength() > 0 &&
            element.getElementsByTagName("PhoneNumber").item(0).getChildNodes().getLength() > 0) {
            this.setPhoneNumber(((Node) element.getElementsByTagName("PhoneNumber").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Text").getLength() > 0 &&
            element.getElementsByTagName("Text").item(0).getChildNodes().getLength() > 0) {
            this.setText(((Node) element.getElementsByTagName("Text").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Title").getLength() > 0 &&
            element.getElementsByTagName("Title").item(0).getChildNodes().getLength() > 0) {
            this.setTitle(((Node) element.getElementsByTagName("Title").item(0).getChildNodes().item(0)).getNodeValue());
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
